# !/usr/bin/python3
# -*- coding: utf-8 -*-
from sklearn.preprocessing import PolynomialFeatures
from sklearn.model_selection import train_test_split
from sklearn.linear_model import LinearRegression
from sklearn.metrics import mean_squared_error
import pandas as pd
import logging as log
#准备数据

df = pd.read_excel('class/the house data2.xlsx')  # 确保文件路径正确
X = df[["the house square","from house to subway station "]].values
y = df['the house price'].values
X_train,X_test,y_train,y_test = train_test_split(X,y,test_size=0.1)

#构建出新的多项式特征矩阵
pf = PolynomialFeatures(degree=2)
pf.fit(X_train)
X_train = pf.transform(X_train)
X_test = pf.transform(X_test)

#创建模型
lr = LinearRegression()

#模型训练
lr.fit(X_train,y_train)

#模型评估
y_predict = lr.predict(X_test)
print(mean_squared_error(y_predict,y_test))
y_train_predict= lr.predict(X_train)
print(mean_squared_error(y_train_predict,y_train)) 

#模型预测(请预测100平,距离地铁站5km的房价）
X_new = pf.transform([[100, 5]])
print(lr.predict(X_new))
'''
通过高次方项的过多加入,最终引入的一定是过拟合问题
所谓过拟合,是指train的数据表现更好,test数据表现极差,且具有数量级的差异
处理:1.减少高次方项,简化模型。2.减少特征项 3. 对训练数据进行更好的清洗 4.增大数据量
所谓欠拟合,是指train和test的指标都不好,也基本持平
处理:1.模型复杂 2.增加高次项
'''